{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "50ff110a",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "- list方法/函数 的所有使用场景及案例\n",
    "  - append()、insert()、extend()\n",
    "  - pop(inedx)、remove(values)\n",
    "  - list[] = \n",
    "  - index()\n",
    "  - count()\n",
    "  - len()\n",
    "  - sort()\n",
    "  - reverse()\n",
    "- list 高级方法：\n",
    "  - copy()、切片[:]  (复制 不等于 赋值)\n",
    "  - enumerate()\n",
    "- 课本练习 \n",
    "  - list的一些方法实现\n",
    "  - list 的切片实现\n",
    "  - str --> list  list()\n",
    "  - list --> str  \"\".join(list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57f06cfc",
   "metadata": {},
   "source": [
    "## 本周内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "7ab7c539",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "# vowels.py\n",
    "vowels = [\"a\", \"e\", \"i\", \"o\", \"u\"]\n",
    "word  = \"Milliways\"\n",
    "# 需求：word中出现的 vowels 打印出来\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "759e88c7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['i', 'a']\n"
     ]
    }
   ],
   "source": [
    "# 新建列表，append常用于新建列表\n",
    "found = []\n",
    "for i in word:\n",
    "    if i in vowels:\n",
    "        if i not in found:\n",
    "            found.append(i)\n",
    "print(found)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9da48b8",
   "metadata": {},
   "source": [
    "## 购物车练习\n",
    " \n",
    "1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3f0c6bb9",
   "metadata": {
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的支付宝余额：1000000\n",
      "-------商品列表-------\n",
      "0 ['华为P30', 3800]\n",
      "1 ['Mac Pro', 16000]\n",
      "2 ['redmi K40', 1999]\n",
      "3 ['iPhone', 8399]\n",
      "4 ['vivo X 70', 4199]\n",
      "5 ['python book', 100]\n",
      "选择商品的序号：1\n",
      "你选择的商品信息是： ['Mac Pro', 16000]\n",
      "【Mac Pro】已添加进入你的购物车，你的【余额】还有984000\n",
      "-------商品列表-------\n",
      "0 ['华为P30', 3800]\n",
      "1 ['Mac Pro', 16000]\n",
      "2 ['redmi K40', 1999]\n",
      "3 ['iPhone', 8399]\n",
      "4 ['vivo X 70', 4199]\n",
      "5 ['python book', 100]\n",
      "选择商品的序号：1\n",
      "你选择的商品信息是： ['Mac Pro', 16000]\n",
      "【Mac Pro】已添加进入你的购物车，你的【余额】还有968000\n",
      "-------商品列表-------\n",
      "0 ['华为P30', 3800]\n",
      "1 ['Mac Pro', 16000]\n",
      "2 ['redmi K40', 1999]\n",
      "3 ['iPhone', 8399]\n",
      "4 ['vivo X 70', 4199]\n",
      "5 ['python book', 100]\n",
      "选择商品的序号：3\n",
      "你选择的商品信息是： ['iPhone', 8399]\n",
      "【iPhone】已添加进入你的购物车，你的【余额】还有959601\n",
      "-------商品列表-------\n",
      "0 ['华为P30', 3800]\n",
      "1 ['Mac Pro', 16000]\n",
      "2 ['redmi K40', 1999]\n",
      "3 ['iPhone', 8399]\n",
      "4 ['vivo X 70', 4199]\n",
      "5 ['python book', 100]\n",
      "选择商品的序号：5\n",
      "你选择的商品信息是： ['python book', 100]\n",
      "【python book】已添加进入你的购物车，你的【余额】还有959501\n",
      "-------商品列表-------\n",
      "0 ['华为P30', 3800]\n",
      "1 ['Mac Pro', 16000]\n",
      "2 ['redmi K40', 1999]\n",
      "3 ['iPhone', 8399]\n",
      "4 ['vivo X 70', 4199]\n",
      "5 ['python book', 100]\n",
      "选择商品的序号：5\n",
      "你选择的商品信息是： ['python book', 100]\n",
      "【python book】已添加进入你的购物车，你的【余额】还有959401\n",
      "-------商品列表-------\n",
      "0 ['华为P30', 3800]\n",
      "1 ['Mac Pro', 16000]\n",
      "2 ['redmi K40', 1999]\n",
      "3 ['iPhone', 8399]\n",
      "4 ['vivo X 70', 4199]\n",
      "5 ['python book', 100]\n",
      "选择商品的序号：q\n",
      "-------购物清单-------\n",
      "Mac Pro : 2 件\n",
      "\n",
      "iPhone : 1 件\n",
      "\n",
      "python book : 2 件\n",
      "\n",
      "你的余额还剩：959401 元\n"
     ]
    }
   ],
   "source": [
    "# 商品清单列表\n",
    "product_lists = [\n",
    "    [\"华为P30\", 3800],\n",
    "    [\"Mac Pro\", 16000],\n",
    "    [\"redmi K40\", 1999],\n",
    "    [\"iPhone\", 8399],\n",
    "    [\"vivo X 70\", 4199],\n",
    "    [\"python book\", 100]\n",
    "]\n",
    "# 购物清单：空列表\n",
    "shopping_lists = []\n",
    "\n",
    "Alipay = input(\"请输入你的支付宝余额：\")\n",
    "\n",
    "# isdigit()判断字符串是否是由数字组成\n",
    "if Alipay.isdigit():\n",
    "    Alipay = int(Alipay)\n",
    "    while True:\n",
    "        # 循环遍历\n",
    "        print(\"-------商品列表-------\")\n",
    "#           缺点：代码杂糅\n",
    "#         info = \"\"\"-------商品列表-------\n",
    "#         商品编号0，商品名称：%s，商品的价格：%s 元\n",
    "#         商品编号1，商品名称：%s，商品的价格：%s 元\n",
    "#         商品编号2，商品名称：%s，商品的价格：%s 元\n",
    "#         商品编号3，商品名称：%s，商品的价格：%s 元\n",
    "#         商品编号4，商品名称：%s，商品的价格：%s 元\n",
    "#         商品编号5，商品名称：%s，商品的价格：%s 元\n",
    "#         \"\"\" % (product_lists[0][0],product_lists[0][1],\n",
    "#                product_lists[1][0],product_lists[1][1],\n",
    "#                product_lists[2][0],product_lists[2][1],\n",
    "#                product_lists[3][0],product_lists[3][1],\n",
    "#                product_lists[4][0],product_lists[4][1],\n",
    "#                product_lists[5][0],product_lists[5][1],\n",
    "#               )\n",
    "#         print(info)\n",
    "#         尝试用for in range()循环来操作\n",
    "#         for i in range(4):\n",
    "#             print(\"编号%s,商品名称：%s，商品的价格：%s 元\" % (i, product_lists[i][0], product_lists[i][1]))\n",
    "#       利用上节课所学习到的枚举法：enumerate() 也可以实现\n",
    "        for index, item in enumerate(product_lists):\n",
    "            print(index, item)\n",
    "            \n",
    "        choice = input(\"选择商品的序号：\")\n",
    "        # 判断输入的是否是数字\n",
    "        if choice.isdigit():\n",
    "            choice = int(choice)\n",
    "            if choice < len(product_lists):\n",
    "                print(\"你选择的商品信息是：\", product_lists[choice])\n",
    "                name = product_lists[choice][0]\n",
    "                price = product_lists[choice][1]\n",
    "                if price <= Alipay:\n",
    "                    Alipay = Alipay - price\n",
    "                    print(\"【%s】已添加进入你的购物车，你的【余额】还有%s\" % (name, Alipay) )\n",
    "                    shopping_lists.append(name)\n",
    "                else:\n",
    "                    print(\"你的余额不足，请选择其他商品或者按'Q'或'q'退出\")\n",
    "            else:\n",
    "                print(\"商城里没有这个东西，你可以按'Q'或'q'退出\")\n",
    "        # 当选择q或Q的时候退出循环并且打印购物清单\n",
    "        elif choice == \"Q\" or choice == \"q\":\n",
    "            print(\"-------购物清单-------\")\n",
    "            shopping_dict = {}\n",
    "            for i in shopping_lists:\n",
    "                if i not in shopping_dict:\n",
    "                    shopping_dict[i] = 1\n",
    "                else:\n",
    "                    shopping_dict[i] += 1\n",
    "            for key, value in shopping_dict.items():\n",
    "                print(key, \":\", value, \"件\\n\")\n",
    "            print(\"你的余额还剩：%s 元\" % Alipay)\n",
    "            break\n",
    "        else:\n",
    "            print(\"请输入编号或者按'Q'或'q'退出\")\n",
    "else:\n",
    "    print(\"请输入正确的支付宝余额！！！\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9cf08610",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
